﻿Imports System.Management

Public Class GetADSite
    Public Shared Function GetAdSite(ByVal strcomputer As String) As String
        Dim wmiScope As ManagementScope
        Dim wmiConnectionOptions As New ConnectionOptions
        Dim query As ManagementObjectSearcher
        Dim oq As ObjectQuery
        Dim strADsite As String = ""
        With wmiConnectionOptions
            .Impersonation = ImpersonationLevel.Impersonate
            .Authentication = AuthenticationLevel.Packet
        End With
        Try
            wmiScope = New ManagementScope("\\" & strcomputer & "\root\ccm\invagt", wmiConnectionOptions)
            oq = New ObjectQuery("Select * from ADSiteName")
            query = New ManagementObjectSearcher(wmiScope, oq)
            For Each oReturn In query.Get()
                strADsite = oReturn.Properties("ADSiteName").Value.ToString()
            Next
            Return strADsite
        Catch
            Return "Active Directory inaccessable"
        End Try
    End Function
End Class
